package com.fzj.example.socket.tcp.nonblock.nio;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * ClassName: NioSelClient
 * date: 2019/11/21 11:39
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class NioSelClient {
    public static void main(String[] args) {
        try {
            SocketChannel sc=SocketChannel.open();
            sc.connect(new InetSocketAddress(InetAddress.getByName("192.168.0.23"),8080));
            ByteBuffer readBuff=ByteBuffer.allocate(1024);
            ByteBuffer writeBuff=ByteBuffer.allocate(1024);
            writeBuff.put("我是微笑".getBytes());
            writeBuff.flip();
            int num=1000;
            while (num>0){
                writeBuff.rewind();
                sc.write(writeBuff);
                readBuff.clear();
                int len=sc.read(readBuff);
                readBuff.flip();
                System.out.println("client received:"+new String(readBuff.array(),0,len));
                num--;
            }
            sc.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
